<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var name = '小王',age = 17;
    var obj = {
      name:'校长',
      objAge:this.age,
      myFun:function(f,t){
        console.log(this.name + '年龄：' + this.age + '来自' +f +'去往'+ t)
      }
    }
    var dv = {
      name:'德玛',
      age:99
    }
    obj.myFun.call(dv,'上海','杭州')
    obj.myFun.apply(dv,['成都','杭州'])
    obj.myFun.bind(dv,'成都','杭州')()

    Function.prototype.myCall = function (context){
      context = context || window;
      context.fn = this
      let args = [...arguments].slice(1)
      var result = context.fn(...args)
      delete context.fn
      return result
    }
    Function.prototype.myApply = function (context){
      var context = context || window
      context.fn = this
      let result = arguments[1] ? context.fn(...arguments[1]) :context.fn()
      console.log(...arguments[1])
      delete context.fn
      return result
    }
    Function.prototype.imitateBind = function (context) {
      let args = [...arguments].slice(1),
      F= function() {},
      self=this,
      bound =function(){
        finallyargs= [...args,...arguments]
        return self.call((this instanceof F ? this : context),...finallyargs)
      }
      F.prototype = self.prototype
      bound.prototype = new F()
      return bound
  }
    let sayHello = function (o){
      console.log(this.name+ '说' + o)
    }
    let a={
      name:' 世界'
    }
    sayHello.myCall(a,'你好')
    sayHello.myApply(a,[3, 4, 5, 6, 7])
    sayHello.imitateBind(a,'你好')()
  </script>
</body>
</html>